package com.showcai.controller.certificate;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.showcai.controller.base.BaseController;
import com.showcai.def.Constant;
import com.showcai.module.CertificateBatch;
import com.showcai.module.CertificateClassify;
import com.showcai.module.CertificateUserdLog;
import com.showcai.result.ResultObj;
import com.showcai.result.ReturnCode;
import com.showcai.service.CertificateBatchService;
import com.showcai.service.CertificateClassifyService;
import com.showcai.service.CertificateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by Baggio on 2017-7-3.
 */
@RequestMapping("/certificateBatch")
@Controller
public class CertificateBatchContorller extends BaseController {
    @Autowired
    CertificateService certificateService;
    @Autowired
    CertificateBatchService certificateBatchService;
    @Autowired
    CertificateClassifyService certificateClassifyService;


    /**
     * 卡劵批次管理
     *
     * @param page
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/certificateBatchList")
    public ModelAndView certificateBatchList(Integer page, Integer pageSize) {
        ModelAndView mov = new ModelAndView();
        mov.setViewName("/certificate/certificateBatchList");
        Map<String, String> map = this.showRequestParameters();
        if (page == null || page == 0) {
            page = Constant.PAGENUM;
        }
        // 设置每页记录数
        if (pageSize == null || pageSize == 0) {
            pageSize = Constant.PAGESIZENum;
        }
        map.put("page", String.valueOf(page));
        map.put("pageSize", String.valueOf(pageSize));
        PageInfo pageInfo = certificateBatchService.getPageInfoCertificateBatchList(map);
        mov.addObject("pageinfo", pageInfo);
        mov.addObject("bean", map);
        return mov;
    }

    /**
     * 跳转到卡劵批次修改页面
     *
     * @param batchId
     * @return
     */
    @RequestMapping(value = "/certificateBatch")
    public ModelAndView certificateBatch(Long batchId) {
        ModelAndView mov = new ModelAndView();
        mov.setViewName("/certificate/certificateBatch");
        if (batchId != null) {
            CertificateBatch certificateBatch = certificateBatchService.getCertificateBathByBatchId(batchId);
            mov.addObject("certificateBatch", certificateBatch);
        }
        List<CertificateClassify> certificateClassifyList = certificateClassifyService.getCertificateClassifyList(null);
        mov.addObject("certificateClassifyList", certificateClassifyList);
        return mov;
    }

    /**
     * 修改批次方法
     *
     * @param request certificateBatch
     * @return
     */
    @RequestMapping(value = "/modifyCertificateBatch", method = RequestMethod.POST)
    @ResponseBody
    public String modifyCertificateBatch(CertificateBatch certificateBatch) {
        ResultObj resultObj = new ResultObj();
        try {
            certificateBatchService.modifyCertificateBatch(certificateBatch);
        } catch (Exception e) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return resultObj.toJson();
    }
}
